﻿using System;
using System.Collections.Generic;
using System.Text;

namespace CPPEI.Coolzon.ViewState
{
    public static class PartStateUtil
    {
        /// <summary>
        /// 
        /// PartState
        /// ex:
        ///     p1?a1=2&a2=3#p2?a1=3
        /// </summary>
        /// <param name="partStateList"></param>
        /// <returns></returns>
        public static string ToString(List<PartState> partStateList)
        {
            if (partStateList == null)
            {
                return string.Empty;
            }

            StringBuilder builder = new StringBuilder();
            for (int index = 0; index < partStateList.Count; index++)
            {
                PartState partState = partStateList[index];
                builder.AppendFormat("{0}?", partState.Name);
                
                foreach(string key in partState.States.Keys)
                {
                    string value = partState.GetString(key);
                    builder.AppendFormat("{0}={1}&", key, value);
                }
                builder.Append("#");
            }

            return builder.ToString();
        }

        /// <summary>
        /// 
        /// PartState
        /// ex:
        ///     p1?a1=2&a2=3#p2?a1=3
        /// </summary>
        /// <param name="partStateString"></param>
        /// <returns></returns>
        public static List<PartState> FromString(string partStateString)
        {
            List<PartState> stateList = new List<PartState>();

            string[] segments = partStateString.Split(new char[] { '#' }, StringSplitOptions.RemoveEmptyEntries);
            for (int index = 0; index < segments.Length; index++)
            {
                string[] stateArr = segments[index].Split(new char[] { '?' }, StringSplitOptions.RemoveEmptyEntries);
                if (stateArr.Length == 2)
                {
                    PartState state = new PartState(stateArr[0]);

                    string[] propertyArr = stateArr[1].Split(new char[] { '&' }, StringSplitOptions.RemoveEmptyEntries);
                    for (int propIndex = 0; propIndex < propertyArr.Length; propIndex++)
                    {
                        string[] kvArr = propertyArr[propIndex].Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries);
                        int equalsFlagIndex = propertyArr[propIndex].IndexOf('=', 0);
                        if (equalsFlagIndex > 0)
                        {
                            state.SetString(propertyArr[propIndex].Substring(0, equalsFlagIndex),
                                propertyArr[propIndex].Substring(equalsFlagIndex + 1, propertyArr[propIndex].Length - equalsFlagIndex - 1));
                        }
                    }
                    stateList.Add(state);
                }
                else
                {
                    PartState state = new PartState(stateArr[0]);
                    stateList.Add(state);
                }
            }
            return stateList;
        }
    }
}
